﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Win32;
using System.Xml;

namespace planning
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private Planning planning;
        private string fichier;

        private Planning Planning
        {
            get { return planning; }
            set
            {
                planning = value;
                List<Horaire> horaires = new List<Horaire>();
                List<Matiere> matieres = new List<Matiere>();
                List<Enseignant> enseignants = new List<Enseignant>();
                List<Salle> salles = new List<Salle>();
                foreach (Ressource r in value.Ressources)
                {
                    if (r is Horaire)
                    {
                        horaires.Add((Horaire)r);
                    }else if( r is Matiere)
                    {
                        matieres.Add((Matiere)r);
                    }else if (r is Enseignant)
                    {
                        enseignants.Add((Enseignant)r);
                    }
                    else if (r is Salle)
                    {
                        salles.Add((Salle)r);
                    }
                }
                listHoraires.ItemsSource = horaires;
                listMatiere.ItemsSource = matieres;
                listEnseignant.ItemsSource = enseignants;
                listSalle.ItemsSource = salles;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Nouveau(object sender, RoutedEventArgs e)
        {
            Planning = new Planning();

        }

        private void Ouvrir(object sender, ExecutedRoutedEventArgs e)
        {
            OpenFileDialog dialogue = new OpenFileDialog();
            if (dialogue.ShowDialog() == true)
            {
                fichier = dialogue.FileName;
                Nouveau(sender, e);
                XmlDocument document = new XmlDocument();
                document.Load(fichier);

                Planning = Planning.fromXml(document.SelectSingleNode("/Planning"));
            }
        }

        private void Enregistrer(object sender, ExecutedRoutedEventArgs e)
        {
            if (fichier == null)
                EnregistrerSous(sender, e);
            else
            {
                XmlDocument document = new XmlDocument();
                document.AppendChild(Planning.toXml(document));
                document.Save(fichier);
            }
        }

        private void EnregistrerSous(object sender, RoutedEventArgs e)
        {
            SaveFileDialog dialogue = new SaveFileDialog();
            if (dialogue.ShowDialog() == true)
            {
                fichier = dialogue.FileName;
                XmlDocument document = new XmlDocument();
                document.AppendChild(Planning.toXml(document));
                document.Save(fichier);
            }
        }

        private void EnregistrerPossible(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = (Planning != null);
        }

        private void Quitter(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void AjouterElement(object sender, RoutedEventArgs e)
        {
            switch (first.SelectedIndex)
            {
                case 0:
                    Semaine semaine = new Semaine();
                    planning.Semaines.Add(semaine);
                    listSemaine.SelectedItem = semaine;
                    break;
                case 1:
                    /*MetteurEnScene metteurEnScene = new MetteurEnScene();
                    filmographie.MetteursEnScene.Add(metteurEnScene);
                    listeMetteursEnScene.SelectedItem = metteurEnScene;*/
                    break;
                case 2:

                    break;
                case 3:
                    break;
            }
        }

        private void AjouterElementPossible(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;// (listJours.SelectedItem != null);
        }

        private void SupprimerElement(object sender, RoutedEventArgs e)
        {
            Enseignement role = (Enseignement)listEnseignant.SelectedItem;
            Jour film = (Jour)listEnseignement.SelectedItem;
            film.Enseignements.Remove(role);
        }

        private void SupprimerElementPossible(object sender, CanExecuteRoutedEventArgs e)
        {
            ListView liste = null;
            if (first != null)
            {
                switch (first.SelectedIndex)
                {
                    case 0:
                        liste = listEnseignementJour;
                        break;
                    case 1:
                        liste = listSalle;
                        break;
                    case 2:
                        liste = listEnseignement;
                        break;
                }
                if (liste != null)
                    e.CanExecute = (Planning != null) && (liste.SelectedItem != null);
                else
                    e.CanExecute = false;
            }
            else
                e.CanExecute = false;
        }

        private void listHoraire_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void listMatiere_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void listEnseignement_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void listEnseignant_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void listSalle_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void listJours_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
        private void HoraireSelectionne(object sendre, SelectionChangedEventArgs e)
        {

        }
    }
}
